//
//  HLDBManager.m
//  伙聊
//
//  Created by ShareAnimation on 2019/8/12.
//  Copyright © 2019 Huoliao. All rights reserved.
//

#import "HLDBManager.h"
#import "RCDDBHelper.h"

@implementation HLDBManager
+ (BOOL)openDB:(NSString *)userId {
    NSString *path = [self createDBPath:userId];
    BOOL result = [RCDDBHelper openDB:path];
    if (result) {
        [self createTableIfNeed];
    }
    return result;
}

+ (void)closeDB {
    [RCDDBHelper closeDB];
}

+ (NSString *)createDBPath:(NSString *)sourcePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *dbPath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"RongCloud"] stringByAppendingPathComponent:sourcePath];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dbPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:dbPath
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:nil];
    }
    
    dbPath = [dbPath stringByAppendingPathComponent:@"HLTalkDB"];
    return dbPath;
}


+ (void)createTableIfNeed {
    if (![RCDDBHelper isDBOpened]) {
        NSLog(@"createTableIfNeed, db is not open");
        return;
    }
    [RCDDBHelper updateTable:@"t_group_manager"
                     version:1
                 transaction:^BOOL(FMDatabase * _Nonnull db) {
                     NSString *sql = @"CREATE TABLE IF NOT EXISTS t_group_manager ("
                     "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                     "user_id TEXT NOT NULL UNIQUE"
                     ")";
                     BOOL result = [db executeUpdate:sql];
                     if (result) {
                         result = [db executeUpdate:@"CREATE INDEX IF NOT EXISTS idx_user_id ON t_group_manager(user_id)"];
                     }
                     return result;
                 }];

}

#pragma mark - 群管理
+ (void)saveManagers:(NSArray <NSString *> *)managerList {
    if (![RCDDBHelper isDBOpened]) {
        NSLog(@"saveManagers, db is not open");
        return;
    }
    NSString *sql = @"REPLACE INTO t_group_manager (user_id) VALUES (?)";
    [RCDDBHelper executeTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
        for (NSString *managerId in managerList) {
            if (managerId.length > 0) {
                [db executeUpdate:sql withArgumentsInArray:@[managerId]];
            }
        }
    }];
}

+ (NSArray <NSString *> *)getManagerList {
    if (![RCDDBHelper isDBOpened]) {
        NSLog(@"getManagerList, db is not open");
        return nil;
    };
    
    __block NSMutableArray *managerList = [NSMutableArray new];
    NSString *sql = @"SELECT * FROM t_group_manager";
    [RCDDBHelper executeQuery:sql
         withArgumentsInArray:nil
                   syncResult:^(FMResultSet *resultSet) {
                       while ([resultSet next]) {
                           NSString *userId = [resultSet stringForColumn:@"user_id"];
                           [managerList addObject:userId];
                       }
                   }];
    return managerList;
}

+ (void)clearManagers {
    if (![RCDDBHelper isDBOpened]) {
        NSLog(@"clearFriends, db is not open");
        return;
    }
    NSString *sql = @"DELETE FROM t_group_manager";
    [RCDDBHelper executeUpdate:sql withArgumentsInArray:nil];
}
@end
